home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / lzhsourc.lzh / LZH.SRC / DECODLZ4.C < prev    next >
C/C++ Source or Header  |  1992-07-02  |  2KB  |  97 lines

  1. /**************************************************************
  2.     lzhuf.c
  3.     written by Haruyasu Yoshizaki 11/20/1988
  4.     some minor changes 4/6/1989
  5.     comments translated by Haruhiko Okumura 4/7/1989
  6.  
  7.     reinserted with modifications into 'lharc.c'
  8.     by J. Moeller 1/30/1990
  9. **************************************************************/
  10.  
  11. #define _hufst_
  12. #ifdef _hufst_
  13.   #define from extern
  14. #else
  15.   #define from
  16. #endif
  17. #ifndef __TOS__
  18. #error Please try assembling 'lzhuf.asm'!
  19. #endif
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25.  
  26. #define RDERR        13
  27. #define WTERR        14
  28. #define MAXBLK        64
  29.  
  30. extern FILE *infile, *outfile;
  31. extern char *infname, *outfname;
  32. extern long textsize, codesize;
  33. extern unsigned crc, crctbl [];
  34. extern unsigned blkcnt;
  35. extern unsigned char flg_n;
  36. extern long blocksize;
  37. extern void error (int errcode, char *p);
  38. #define setcrc(ch) (crc = (crc >> 8) ^ crctbl [(crc ^ (ch)) & 0xff])
  39.  
  40. /********** Larc-Decompression **********/
  41.  
  42. #define N        4096    /* buffer size */
  43. #define F        18    /* lookahead buffer size */
  44. #define THRESHOLD    2
  45. #define NIL    N    /* leaf of tree */
  46. #define FOLD        18   /* upper limit for match_length */
  47.  
  48. typedef unsigned char uchar;
  49.  
  50. from unsigned char  text_buf[N + F - 1];
  51. unsigned long         printcount;
  52.  
  53. void DecodeOld(void)       /* Just the reverse of Encode(). */
  54. {
  55.     register int  i, j, k, r, c;
  56.     register unsigned int  flags;
  57.     long int todo=codesize,done=N;
  58.     for (i = 0; i < N - F; i++) text_buf[i] = ' ';
  59.     r = N - F;  flags = 0;
  60.     for ( ; ; ) {
  61.         if (((flags >>= 1) & 256) == 0) {
  62.             c = getc(infile);
  63.             if (todo-- == 0) break;
  64.             flags = c | 0xff00;
  65.                 /* uses higher byte cleverly */
  66.         }                            /* to count eight */
  67.         if (flags & 1) {
  68.             if (todo-- == 0) break;
  69.             c = getc(infile);
  70.             if (outfile !=NULL) putc(c, outfile);  text_buf[r++] = c;
  71. #ifdef _shell_
  72.             setcrc(c);
  73.             if (done-- == 0) {
  74.               if (!flg_n) putchar('*'); done=N;}
  75. #endif
  76.             r &= (N - 1);
  77.         } else {
  78.             if (todo-- == 0) break;
  79.             i = getc(infile);
  80.             if (todo-- == 0) break;
  81.             j = getc(infile);
  82.             i |= ((j & 0xf0) << 4);
  83.             j = (j & 0x0f) + THRESHOLD;
  84.             for (k = 0; k <= j; k++) {
  85.                 c = text_buf[(i + k) & (N - 1)];
  86.                 if (outfile !=NULL) putc(c, outfile);
  87.                 setcrc(c);
  88.                 text_buf[r++] = c;  r &= (N - 1);
  89.                 if (done-- == 0) {
  90.                    if (!flg_n) putchar('*'); done=N;}
  91.             }
  92.         }
  93.     }
  94. }
  95.  
  96.  
  97.